//
// Created by wujehy on 2020/8/3.
//

#include "TCPServer.h"
#include "ContextDataType.h"
#include "MyCodecType.h"
#include <handy/handy.h>
#include <Network.pb.h>
#include "../NetWorkWarpper.h"

using handy::Logger;

void TCPServer::init()
{
    using namespace handy;
    m_serverPtr = handy::TcpServer::startServer( multiBase, serverIp, server_port );
    exitif( m_serverPtr == NULL, "start tcpserver failed" );
    m_serverPtr->onConnCreate(
            [&]
            {
                info( "new client " );
                // 新的客户端接入
                TcpConnPtr n_client( new TcpConn );
                // 链接管理
                n_client->onState( netWorkWarpper->getOnConnectCallback());
                // 添加事件
                n_client->onMsg( new MyCodecType, netWorkWarpper->getOnMessageCallback());
                info( "new client end" );
                return n_client;
            } );

}

void TCPServer::deinit()
{

}

TCPServer::~TCPServer()
{
    info( "delete TCPServer" );
}

void TCPServer::setServerIp(const std::string &serverIp)
{
    TCPServer::serverIp = serverIp;
}

void TCPServer::setServerPort(int serverPort)
{
    server_port = serverPort;
}

